package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.d3rich.sliderlistview.SliderListView;
import com.d3rich.sliderlistview.SliderMenu;
import com.d3rich.sliderlistview.SliderMenuCreator;
import com.d3rich.sliderlistview.SliderMenuItem;
import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.MyDemandEntitiy;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.User;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.adapter.ItemLvMyDemandActivityAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/*
   已发布详情的界面 查看自己的已发布的信息
 */
public class MyDemandActivity extends BaseActivity {

    @Bind(R.id.tv_title_my_order_activity)
    TextView mTvTitleMyOrderActivity;
    @Bind(R.id.tv_title1_my_order_activity)
    TextView mTvTitle1MyOrderActivity;
    @Bind(R.id.ll_title_my_demand_activity)
    LinearLayout mLlTitleMyDemandActivity;
    @Bind(R.id.rl_background_my_demand)
    RelativeLayout mRlBackGroundMyDemand;
    @Bind(R.id.imagv_background_my_demand)
    ImageView mImagvBackgroundMyDemand;
    @Bind(R.id.tv_background_my_demand)
    TextView mTvBackgroundMyDemand;
    @Bind(R.id.tv_goToFaBuPinTai_background_my_demand)
    TextView mTvGoToFaBuPinTaiBackgroundMyDemand;
    @Bind(R.id.tv_goToChuShouPinTai_background_my_demand)
    TextView mTvGoToChuShouPinTaiBackgroundMyDemand;
    //====================================
    ItemLvMyDemandActivityAdapter mAdapter;
    public static final int SUPPLY_TYPE = 0;
    public static final int DEMAND_TYPE = 1;
    @Bind(R.id.slv_my_order_activity)
    SliderListView mSlvMyOrderActivity;
    MyDemandEntitiy mMyDemandEntitiy = null;
    List<MyDemandEntitiy.ListEntity> mEntityList;
    Gson mGson = new Gson();
    User mUser;
    String user_id;
    String TAG = "MyDemandAvtivity";
    int isClickName;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            System.out.println(TAG + msg.obj.toString());
            if (msg.arg2 != 2) {
                mRlBackGroundMyDemand.setVisibility(View.GONE);
                if (msg.arg1 == NetConfig.TaskId.closeMessageSend) {
                    NetWorking();
                }
                if (msg.arg1 == NetConfig.TaskId.messageSend) {
                    mMyDemandEntitiy = mGson.fromJson(msg.obj.toString(), MyDemandEntitiy.class);
                    mEntityList = mMyDemandEntitiy.getList();
                    appearData();
                } else {
                    mEntityList = new ArrayList<>();
                    if (mEntityList.size() == 0) {
                        changeButton();
                    }

                }
            } else if (msg.obj.toString().trim().equals("暂无数据")) {
                mEntityList = new ArrayList<>();
                if (mEntityList.size() == 0) {
                    changeButton();
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_demand);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, 0, 0, 0);
        }
        tvTitle.setText("已发布信息");
        mUser = new UserImpl(this).getUser();
        user_id = mUser.id;
        addViewClicklistener(tvBack, mTvTitleMyOrderActivity, mTvTitle1MyOrderActivity);
        isClickName = SUPPLY_TYPE;
    }

    public void NetWorking() {
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER + NetConfig.TYPE_USER,
                NetConfig.messageSend, mHandler, "jsonObject", NetConfig.TaskId.messageSend, new Object[]{isClickName, mUser.token});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    @Override
    protected void onResume() {
        super.onResume();
        NetWorking();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            //求购信息
            case R.id.tv_title_my_order_activity:
                isClickName = SUPPLY_TYPE;
                NetWorking();
                mTvTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
                mTvTitle1MyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_default));
                break;
            //出售信息
            case R.id.tv_title1_my_order_activity:
                isClickName = DEMAND_TYPE;
                NetWorking();
                mTvTitle1MyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
                mTvTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_default));
                break;
            default:
                break;
        }
    }

    public void appearData() {
        mAdapter = new ItemLvMyDemandActivityAdapter(this, mEntityList);
        mSlvMyOrderActivity.setAdapter(mAdapter);
        SliderMenuCreator creator = new SliderMenuCreator() {
            @Override
            public void create(SliderMenu menu) {
                switch (menu.getViewType()) {
                    case 0:
                        cretaMenu1(menu);
                        break;
                    case 1:
                        cretaMenu2(menu);
                        break;
                }
            }

            private void cretaMenu1(SliderMenu menu) {
                //添加编辑按钮的图片
                SliderMenuItem item = new SliderMenuItem(MyDemandActivity.this);
                item.setWidth(dip2px(MyDemandActivity.this, 75));
                item.setHeight(dip2px(MyDemandActivity.this, 90));
                item.setBackground(getResources().getDrawable(R.mipmap.ic_useredit_edit));
                menu.addMenuItem(item);
                //添加删除按钮的图片
                SliderMenuItem item1 = new SliderMenuItem(MyDemandActivity.this);
                item1.setWidth(dip2px(MyDemandActivity.this, 75));
                item1.setHeight(dip2px(MyDemandActivity.this, 90));
                item1.setBackground(getResources().getDrawable(R.mipmap.ic_useredit_delete));
                menu.addMenuItem(item1);
            }
            //其他类型的Menu 没有启用 暂时搁置
            private void cretaMenu2(SliderMenu menu) {
                SliderMenuItem item = new SliderMenuItem(MyDemandActivity.this);
                item.setWidth(dip2px(MyDemandActivity.this, 90));
                item.setHeight(dip2px(MyDemandActivity.this, 90));
                item.setBackground(getResources().getDrawable(R.mipmap.ic_useredit_delete));
                menu.addMenuItem(item);
            }
        };
        mSlvMyOrderActivity.setMenuCreator(creator);
        mSlvMyOrderActivity.setOnMenuItemClickListener(new SliderListView.OnMenuItemClickListener() {
            // arg0 是SwipeMenuListView的parent的position arg2是 每个MenuItem中item的position
            @Override
            public boolean onMenuItemClick(int arg0, SliderMenu arg1, int arg2) {
                position = arg0;
                Log.d("MyDemandActivity.arg0", String.valueOf(arg0));
                Log.d("MyDemandActivity.arg2", String.valueOf(arg2));
                switch (arg2){
                    //点击编辑的时候跳转到 编辑的界面
                    case 0:
                        //发布求购信息页
                        if(isClickName==SUPPLY_TYPE){
                            //MyDemandActivity.this.startActivity(new Intent(MyDemandActivity.this,???));
                            Intent intent = new Intent(MyDemandActivity.this, ReleaseInfoActivity.class);
                            intent.putExtra("type",ReleaseInfoActivity.TYPE_DEMAND);
                            intent.putExtra("id",mEntityList.get(arg0).getId());
                            startActivity(intent);
                        }else if(isClickName==DEMAND_TYPE){//发布出售信息页
                            Intent intent = new Intent(MyDemandActivity.this, ReleaseInfoActivity.class);
                            intent.putExtra("type",ReleaseInfoActivity.TYPE_SUPPLY);
                            intent.putExtra("id",mEntityList.get(arg0).getId());
                            startActivity(intent);
                        }
                        break;
                    //点击删除的时候 将信息在列表中删除
                    case 1:
                        showDialog();

                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        mSlvMyOrderActivity.setAdapter(mAdapter);
    }
    Dialog dialog;

    private void showDialog() {
        dialog = new Dialog(this, R.style.dialog_bgTransparent);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.getWindow().setContentView(R.layout.dialog_tel);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
        tv_title.setText("温馨提示");
        TextView tv_contact = (TextView) dialog.findViewById(R.id.tv_dialog_contact);
        tv_contact.setText("确定要删除此订单吗？");
        TextView tv_cancle = (TextView) dialog.findViewById(R.id.tv_dialog_tel_cancle);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView tv_tel = (TextView) dialog.findViewById(R.id.tv_dialog_call);
        tv_tel.setText("确定");
        tv_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(MyDemandActivity.this, NetConfig.SERVER + NetConfig.TYPE_USER,
                        NetConfig.closeMessageSend, mHandler, "jsonObject", NetConfig.TaskId.closeMessageSend, new Object[]{mEntityList.get(position).getId(), new UserImpl(MyDemandActivity.this).getUser().token});
                jsonRPCAsyncTask.execute(new Void[]{});
            }
        });
    }

    int position;

    public void changeButton() {
        mRlBackGroundMyDemand.setVisibility(View.VISIBLE);
        if (isClickName == 0) {
            mTvGoToChuShouPinTaiBackgroundMyDemand.setVisibility(View.GONE);
            mTvGoToFaBuPinTaiBackgroundMyDemand.setVisibility(View.VISIBLE);
            mTvGoToFaBuPinTaiBackgroundMyDemand.setText("点击去平台发布求购信息");
            mTvGoToFaBuPinTaiBackgroundMyDemand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyDemandActivity.this, ReleaseInfoActivity.class);
                    intent.putExtra("type", "0");
                    MyDemandActivity.this.startActivity(intent);
                }
            });
        } else {
            mTvGoToFaBuPinTaiBackgroundMyDemand.setVisibility(View.GONE);
            mTvGoToChuShouPinTaiBackgroundMyDemand.setVisibility(View.VISIBLE);
            mTvGoToChuShouPinTaiBackgroundMyDemand.setText("点击去平台发布出售信息");
            mTvGoToChuShouPinTaiBackgroundMyDemand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyDemandActivity.this, ReleaseInfoActivity.class);
                    intent.putExtra("type", "1");
                    MyDemandActivity.this.startActivity(intent);
                }
            });
        }

    }

    // dip转成px
    public static int dip2px(Context context, float dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }
}
